<!--suppress ALL -->
<template>
  <div class="login">
    <div class="login_box">
      <!--头像-->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt=""></img>
      </div>
      <!--      登陆表单   userRulesRef 引用，调用重置方法使用-->
      <el-form :rules="userRules" ref="userRulesRef" label-width="0px" class="login_form" :model="user">

        <el-form-item prop="username">
          <el-input prefix-icon="iconfont icon-kehuguanli" v-model="user.username">
          </el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input show-password prefix-icon="iconfont icon-mima" v-model="user.password">
          </el-input>
        </el-form-item>

        <el-form-item class="btn">
          <el-button type="primary" :plain="true" @click="login">登陆</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>

      </el-form>


    </div>
  </div>
</template>

<script>
  export default {
    name: "Login",
    data() {
      return {
        user: {
          username: 'admin',
          password: '123456'
        },
        userRules: {
          username: [
            {required: true, message: '请输入登陆账号', trigger: 'blur'},
            {min: 4, max: 12, message: '长度在 4 到 12 个字符', trigger: 'blur'}
          ],
          password: [
            {required: true, message: '请输入登陆密码', trigger: 'blur'},
            {min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      reset() {
        this.$refs.userRulesRef.resetFields();
      },
      async login() {
        //表单预验证
        await this.$refs.userRulesRef.validate(valid => {
          if (valid) {
            this.myAxios.get("/vueUser/user/login", {
                params: this.user
              }
            ).then(resonse => {
              console.log(resonse);
              if (resonse.code === 200) {
                // 保存token
                // window.sessionStorage.setItem("token", resonse.code);
                window.sessionStorage.setItem("token", resonse.result.token);
                this.$router.push('/home').catch(err =>
                  console.log(err));

                // 消息弹出this.$message的两种使用
                return this.$message({
                  message: resonse.message,
                  type: 'success'
                })
              }
              return this.$message.error(resonse.message);
            })
          }
        })
      }

    }

  }
</script>

<style lang="less" scoped>
  .login {
    background-color: #416d8b;
    height: 100%;
  }

  .login_box {
    width: 450px;
    height: 300px;
    background-color: aliceblue;
    border-radius: 5px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .avatar_box {
      height: 130px;
      width: 130px;
      border-radius: 50%;
      border: 1px solid gainsboro;
      padding: 8px;
      box-shadow: 0 0 10px ghostwhite;
      background-color: aliceblue;
      position: absolute;
      /*移动百分50%*/
      left: 50%;
      /*移动自身x方向-50%    负y方向 -50%*/
      transform: translate(-50%, -50%);

      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #cac6cc;
      }
    }

    .btn {
      display: flex;
      justify-content: flex-end;
    }

    .login_form {
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 0 20px;
      box-sizing: border-box;
    }
  }

</style>
